package com.base.lib.adapter.vh;

import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @version V1.0
 * @description:
 * @title: Expands
 * @package com.base.lib.adapter
 * @author: zhangwei
 * @date: 2015-12-16 下午2:59
 */
public class ViewHolder {
    private SparseArray<View> viewHolder;
    private View view;

    public static ViewHolder getViewHolder(View view) {
        ViewHolder rcViewHolder = (ViewHolder) view.getTag();
        if (rcViewHolder == null) {
            rcViewHolder = new ViewHolder(view);
            view.setTag(rcViewHolder);
        }
        return rcViewHolder;
    }

    private ViewHolder(View view) {
        this.view = view;
        viewHolder = new SparseArray<View>();
        view.setTag(viewHolder);
    }

    public <T extends View> T findById(int id) {
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }

    public View getConvertView() {
        return view;
    }

    public TextView getTextView(int id) {

        return findById(id);
    }

    public Button getButton(int id) {

        return findById(id);
    }

    public ImageView getImageView(int id) {
        return findById(id);
    }

    public void setTextView(int id, CharSequence charSequence) {
        getTextView(id).setText(charSequence);
    }


}